﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Drawing;
using System.IO;

namespace GGSharp.Structures
{
    public class MessageRichTextFormat : StuctureBase
    {
        protected const byte FONT_BOLD = 0x01;
        protected const byte FONT_ITALIC = 0x02;
        protected const byte FONT_UNDERLINE = 0x04;
        protected const byte FONT_COLOR = 0x08;
        protected const byte FONT_IMAGE = 0x80;

        private short _position;	/* pozycja atrybutu w tekście */
        private byte _font;	/* atrybuty czcionki */

        private Color _color;
        private MessageRichTextImage _image;

        public MessageRichTextFormat() { }
        public MessageRichTextFormat(BinaryReader reader) { Read(reader); }

        public Color Color
        {
            get { return _color; }
        }

        public MessageFormatType FormatType
        {
            get { return TranslateMessageFormat(_font); }
        }

        public short Position
        {
            get { return _position; }
        }

        protected MessageFormatType TranslateMessageFormat(byte font)
        {
            try
            {
                return (MessageFormatType)font;
            }
            catch(Exception e)
            {
                return MessageFormatType.Normal;
            }
        }


        public override void Read(System.IO.BinaryReader reader)
        {
            _position = reader.ReadInt16();
            _font = reader.ReadByte();

            this.ReadedLength = 3;

            if ((_font & FONT_COLOR) > 0)
            {
                byte[] rgb = reader.ReadBytes(3);
                _color = Color.FromArgb(rgb[0], rgb[1], rgb[2]);

                this.ReadedLength += 3;
            }

            if ((_font & FONT_IMAGE) > 0)
            {
                _image = new MessageRichTextImage(reader);

                this.ReadedLength += _image.ReadedLength;
            }
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            throw new NotImplementedException();
        }
    }
}
